<?php
include 'mysql.inc.php';
$id = $_POST['id'];
$cookie = $_COOKIE["vote"];
if (isset($cookie)) {
	$result = array("res" => 'exist');
	echo json_encode($result);
	exit;
}
if (!isset($id)) {
	$result = array("res" => 'error');
	echo json_encode($result);
	exit;
}
$sql = "select * from `star` where id='{$id}'";
$rs = query($sql);
$star = fetch_one($rs);

if(!$star) {
	$result = array("res" => 'error');
	echo json_encode($result);
	exit;
}

query('BEGIN');
query('SET AUTOCOMIIT=0');
try {
	$sql = "SELECT * FROM `star` WHERE id='{$id}' FOR UPDATE";
	$rs = query($sql);
	$row = fetch_one($rs);
	
	$sql = "update `star` set `vote`=`vote` + 1 where id='{$id}'";
	query($sql);
	query('COMMIT');
} catch(Exception $e) {
	query('ROLLBACK');
	echo $e;
}
query('SET AUTOCOMMIT=1');

$sql = "select * from `star` order by id";
$rs = query($sql);
$datas = fetch($rs);



$sql = "select sum(vote) as v from `star`";
$rs = query($sql);
$row = mysql_fetch_assoc($rs);
$total = $row['v'];

$result = array("res" => 'success', "total" => $total, "datas" => $datas);
echo json_encode($result);
closeConnection();
?>